﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using ServiceSecurity;
using BusinessEntities;

namespace YEMSBackendService
{

    public class BLService : IBLS
    {
        private IServiceSecurity _security;
        private BusinessLayer.IBL _bl;
        public BLService()
        {
            _bl = new BusinessLayer.LocalBusinessLayer();
            _security = ServiceSecurityFactory.GetServiceSecurity();
        }

        public ModelValidationReport<BusinessEntities.Model.User> LoginUser(string email, string password, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.LoginUser(email, password);
        }

        public string CreateActivationHash(BusinessEntities.Model.User user, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.CreateActivationHash(user);
        }

        public BusinessEntities.Model.User ValidateActivationHash(string hash, int userId, string hash1)
        {
            if (!_security.ValidateHash(hash1))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.ValidateActivationHash(hash, userId);
        }

        public ModelValidationReport<BusinessEntities.Model.User> CreateUser(BusinessEntities.Model.User user, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.CreateUser(user);
        }

        public BusinessEntities.Model.User GetUserByID(int id, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetUserByID(id);
        }

        public BusinessEntities.Model.User GetUserByEmail(string email, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetUserByEmail(email);
        }

        public BusinessEntities.Model.Hall[] GetAllHalls(string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetAllHalls().ToList().Select(h => TransferHelper.TransferHall(h)).ToArray();
        }

        public BusinessEntities.Model.Hall GetHallById(int id, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferHall(_bl.GetHallById(id));
        }

        public List<BusinessEntities.Model.City> GetCitiesByKeyWord(string keyWord, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetCitiesByKeyWord(keyWord).ToList();
        }

        public BusinessEntities.Model.City GetCityByName(string name, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetCityByName(name);
        }

        public BusinessEntities.Model.City GetCityByID(int id, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetCityByID(id);
        }

        public ModelValidationReport<BusinessEntities.Model.Event> CreateEvent(BusinessEntities.Model.Event aEvent, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var report = _bl.CreateEvent(aEvent);
            report.ModelObject = TransferHelper.TransferEvent(report.ModelObject);
            return report;
        }

        public BusinessEntities.Model.Event GetEventById(int id, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferEvent(_bl.GetEventById(id));
        }

        public List<BusinessEntities.Model.Event> GetUsersEvents(BusinessEntities.Model.User user, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetUsersEvents(user).ToList().Select(e => TransferHelper.TransferEvent(e)).ToList();
        }

        public BusinessEntities.Model.EventReport GetEventReport(BusinessEntities.Model.Event aEvent, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var rep = _bl.GetEventReport(aEvent);
            rep.Event = TransferHelper.TransferEvent(rep.Event);
            return rep;
        }

        public void DeleteEvent(BusinessEntities.Model.Event aEvent, string hash)
        {
            _bl.DeleteEvent(aEvent);
        }

        public ModelValidationReport<BusinessEntities.Model.Event> UpdateEvent(BusinessEntities.Model.Event _event, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var report = _bl.UpdateEvent(_event);
            report.ModelObject = TransferHelper.TransferEvent(report.ModelObject);
            return report;
        }

        public BusinessEntities.Model.Guest GetUsersGuestByEmail(string email, int userid, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferGuest(_bl.GetUsersGuestByEmail(email, userid));
        }

        public List<BusinessEntities.Model.Guest> GetUsersGuests(BusinessEntities.Model.User user, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetUsersGuests(user).ToList().Select(g => TransferHelper.TransferGuest(g)).ToList();
        }

        public ModelValidationReport<BusinessEntities.Model.Guest> CreateGuest(BusinessEntities.Model.Guest guest, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var report = _bl.CreateGuest(guest);
            report.ModelObject = TransferHelper.TransferGuest(report.ModelObject);
            return report;
        }

        public BusinessEntities.Model.Guest GetGuestByID(int id, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferGuest(_bl.GetGuestByID(id));
        }

        public List<BusinessEntities.Model.Invite> GetGuestsInvites(BusinessEntities.Model.Guest g, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetGuestsInvites(g).ToList().Select(i => TransferHelper.TransferInvite(i)).ToList();
        }

        public List<BusinessEntities.Model.Guest> SearchForGuests(string keyword, int userid, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.SearchForGuests(keyword, userid).ToList().Select(g => TransferHelper.TransferGuest(g)).ToList();
        }

        public List<BusinessEntities.Model.Guest> SearchForNonInvitedGuests(string keyword, int eventID, int p, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.SearchForNonInvitedGuests(keyword, eventID, p).ToList().Select(g => TransferHelper.TransferGuest(g)).ToList();
        }

        public void SendMail(string toAddress, string body, string subject, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var message = new System.Net.Mail.MailMessage();
            message.To.Add(toAddress);
            message.Body = body;
            message.Subject = subject;
            _bl.SendMail(message);
        }

        public ModelValidationReport<BusinessEntities.Model.Invite> CreateInvite(BusinessEntities.Model.Invite invite, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var report = _bl.CreateInvite(invite);
            report.ModelObject = TransferHelper.TransferInvite(report.ModelObject);
            return report;
        }

        public List<BusinessEntities.Model.Invite> GetInvitesByEvent(BusinessEntities.Model.Event ev, string hash)
        {
            return _bl.GetInvitesByEvent(ev).ToList().Select(i => TransferHelper.TransferInvite(i)).ToList();
        }

        public BusinessEntities.Model.Invite GetInviteByEventAndGuest(BusinessEntities.Model.Event ev, BusinessEntities.Model.Guest guest, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferInvite(_bl.GetInviteByEventAndGuest(ev, guest));
        }

        public ModelValidationReport<BusinessEntities.Model.Invite> UpdateInvite(BusinessEntities.Model.Invite invite, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            var report = _bl.UpdateInvite(invite);
            report.ModelObject = TransferHelper.TransferInvite(report.ModelObject);
            return report;
        }

        public BusinessEntities.Model.Invite GetInviteById(int id, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferInvite(_bl.GetInviteById(id));
        }

        public BusinessEntities.Model.Invite GetInviteByHash(string hash, int guestID, string hash1)
        {
            if (!_security.ValidateHash(hash1))
            {
                throw new UnauthorizedAccessException();
            }
            return TransferHelper.TransferInvite(_bl.GetInviteByHash(hash, guestID));
        }


        public bool DeleteInvite(int eventID, int inviteID, int guestID, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.DeleteInvite(eventID, inviteID, guestID);
        }


        public void ResendActivationEmail(int userid, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            _bl.ResendActivationEmail(userid);
        }


        public decimal GetAverageGuestGifts(BusinessEntities.Model.Guest guest, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetAverageGuestGifts(guest);
        }


        public List<BusinessEntities.Model.User> GetAllUsers(string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.GetAllUsers();
        }


        public void DeleteUser(BusinessEntities.Model.User user, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            _bl.DeleteUser(user);
        }


        public ModelValidationReport<BusinessEntities.Model.User> UpdateUser(BusinessEntities.Model.User user, string hash)
        {
            if (!_security.ValidateHash(hash))
            {
                throw new UnauthorizedAccessException();
            }
            return _bl.UpdateUser(user);
        }

        public int GetKey(string passcode)
        {
            return 0;
        }
    }
}
